package com.java.config;

import com.java.util.ResultVo;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

@ControllerAdvice
public class GlobalExceptionHandler { 

    @ExceptionHandler(Exception.class)  
    @ResponseBody
    public ResultVo handleException(Exception e, HttpServletResponse response) {
        // 设置状态码为500
        response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
        e.printStackTrace();
        return ResultVo.failure(e.toString());
    }

}